<template>
  <div>
    <Detail ref="detail"></Detail>
    <ZgNewPage
      ref="page"
      :showInlineBtn="false"
      :uniqueName="uniqueName"
      :pageOpt="pageOpt"
      :dataBtns="dataBtns"
      :headers="headers"
      :conditions="conditions"
      :adminWidth="260"
      @search="onSearch"
      @filter="onFilter"
      @header="mergeHeader"
      @admin="toAdmin"
    >
      <template slot="columns">
        <a-table-column
          v-for="(h,i) in headers"
          :key="`column${i}`"
          :width="h.width===0 ? 'auto' : h.width"
          align="center"
          :title="h.label">
          <template slot-scope="record">
            {{ record[h.code] }}
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
  </div>
</template>
<script>
import Detail from './Detail.vue'
export default {
  components: {
    Detail
  },
  data () {
    return {
      uniqueName: 'test',
      pageOpt: {
        showPageTitle: false,
        showConditionBtn: true,
        showHeaderConfig: true,
        showAttachBtn: false
      },
      conditions: [
        { code: 'name1', compType: '单行文本', label: '评分模型', defaultVal: '', extra: { placeholder: '请输入评价模型' } },
        { code: 'birth', compType: '日期区间', label: '执行时间', defaultVal: ['', ''] }
      ],
      headers: [
        { code: '1', label: '评价模型', isShow: true, sort: 0 },
        { code: '2', label: '说明', isShow: true, sort: 0 },
        { code: '3', label: '执行周期', isShow: true, sort: 0 },
        { code: '4', label: '执行时间', isShow: true, sort: 0 },
        { code: '7', label: '企业数量', isShow: true, sort: 0 },
        { code: '5', label: '状态', isShow: true, sort: 0 },
        { code: '6', label: '执行方式', isShow: true, sort: 0 }
      ],
      dataBtns: [
        { key: 1, name: '查看', type: 'link', isShow: () => { return true } },
        { key: 2, name: '确认', type: 'link', isShow: (row) => ['未确认'].includes(row['5']) },
        { key: 3, name: '作废', type: 'link', isShow: (row) => ['未确认'].includes(row['5']) }
      ]
    }
  },
  methods: {
    search () {
      const pageRef = this.$refs.page
      if (pageRef) {
        pageRef.search()
      } else {
        setTimeout(() => {
          this.search()
        }, 50)
      }
    },
    onSearch (param) {
      const diyHeaders = localStorage.getItem(this.currentUser.username + '_diyHeader_' + this.uniqueName)
      if (diyHeaders) {
        this.mergeHeader(JSON.parse(diyHeaders))
      }
      // 加载表头
     const list = [
        { key: '1', id: '1', '1': '模型名称', '2': '此模型部分风险', '3': '每月一次', '4': '2025-01-01', '5': '未确认', '6': '2025-01-01', '7': '55' },
        { key: '2', id: '2', '1': '模型名称', '2': '此模型部分风险', '3': '每月一次', '4': '2025-01-01', '5': '已确认', '6': '2025-01-01', '7': '66' }
    ]
      this.$refs.page.endSearch({
        total: list.length,
        list: list
      })
    },
    toAdmin (opt) {
      if (opt.key === 1) {
        this.$refs.detail.open('detail', opt.obj)
      } else if (opt.key === 2) {
        this.$refs.detail.open('sure', opt.obj)
      } else if (opt.key === 3) {
        this.confirm(
            () => {
            },
            '确定要作废这条数据么')
        }
    },
    onFilter (f, param) {
    },
    mergeHeader (newHeaders) {
      if (!newHeaders) {
        return
      }
      this.headers = newHeaders.filter(t => {
        return t.isShow
      }).sort((a, b) => {
        return a.sort - b.sort
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
